//
//  ViewController.m
//  HitList
//
//  Created by Mac on 15/11/13.
//  Copyright © 2015年 riverlj. All rights reserved.
//

#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import "Person.h"
@interface ViewController () <UITableViewDataSource,UITabBarDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *names;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.names = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
 
}
- (IBAction)addNum:(UIBarButtonItem *)sender {
    UIAlertController *alertViewCtl = [UIAlertController alertControllerWithTitle:@"new name" message:@"add a new name" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertViewCtl addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"new name";
    }];
    
    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *newNameTF = alertViewCtl.textFields.firstObject;
//        [self.names addObject:newNameTF.text];
        [self saveData:newNameTF.text];
        [self.tableView reloadData];
        
    }];
    
    UIAlertAction *cencelAction = [UIAlertAction actionWithTitle:@"cencel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertViewCtl addAction:saveAction];
    [alertViewCtl addAction:cencelAction];

    [self presentViewController:alertViewCtl animated:YES completion:nil];
    
                                       
                                       
}

-(void)viewWillAppear:(BOOL)animated{
    [self fetchData];
}

/**
 *  保存数据
 *
 *  @param name
 */
-(void)saveData:(NSString *)name{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [delegate managedObjectContext];
    Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
    person.name = name;
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"保存数据失败，错误信息为：error=%@",error);
    }
}

/**
 *  获取数据
 */
- (void)fetchData{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [delegate managedObjectContext];
    
    NSFetchRequest * request = [[NSFetchRequest alloc]init];
    request.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
    NSError *error = nil;
    NSArray *array = [context executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"获取数据失败，错误信息是：error=%@",error);
    }
    for (Person *person in array) {
        [self.names addObject:person.name];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.names.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = self.names[indexPath.row];
    
    return cell;
}

@end
